#ifndef __THREAD_HPP__
#define __THREAD_HPP__

#include <string>
#include <cstdio>
#include <cstring>
#include <pthread.h>

typedef void*(*fun_t)(void*);

struct ThreadData
{
    std::string _name;
    void* args;
};

//对于线程操作进行封装，上层要给线程编号，回调方法，回调参数
class Thread
{
public:
    Thread(int num,fun_t callback,void* args)
        :_fun(callback)
    {
        char buff[64];
        memset(buff,0,sizeof(buff));
        snprintf(buff,sizeof(buff),"Thread-%d",num);//线程tid不太好观看，自定义线程name
        _name=buff;

        _thread_data._name=_name;
        _thread_data.args=args;
    }

    //给上层提供更为简单的创建线程，等待线程的接口
    void create()
    {
        pthread_create(&_tid,nullptr,_fun,(void*)&_thread_data);
    }

    void join()
    {
        pthread_join(_tid,nullptr);
    }

    ~Thread()
    {}
private:
    pthread_t _tid;
    std::string _name;
    fun_t _fun;
    ThreadData _thread_data;
};

#endif